<?php
/**
 * Created by PhpStorm.
 * User: Well_Done
 * Date: 2016/2/21
 * Time: 18:14
 */

class SignUpUserAction extends Action
{

    /**
     * 接口名称：m=Activity&a=getSignUpUserList 获取报名用户列表
     * param :
     * atvId: //活动ID
     * selectMode : //选择方式（”selectedmode”--已选名单、”allmode”--全选名单）
     * pageSize: //页长，默认50条
     * pageNum: //页数，从1开始计数，若pagenum=2，则返回第51-100条数据
     * filterCdn: { "index" :6, "value":"A"} //筛选条件，示例数据表示表格中第六列的自定义问题答案为A；若为空则不做筛选
     * orderCdn:{ "index":1, "type":"asc"} //排序条件，示例数据表示按第1列正序排列；若为空则以时间做排序
     */
    public function getSignUpUserList($atvId, $selectMode='allmode', $pageSize=50, $pageNum=0, $filterCdn='', $orderCdn='')
    {
        //查询自定义问题
        $atvOps = M('atv_ops');
        $sql = "SELECT question,ops_str,ops_type FROM fj_atv_ops where atv_id='$atvId' and `index` in (1,2) order by `index`";
        $atvOpsData = $atvOps->query($sql);
        $atvOpsData_opsStr_0 = array();
        $atvOpsData_opsStr_1 = array();
        if( $atvOpsData[0]['ops_str'] )
        {
            $atvOpsData_opsStr_0 = explode("#",$atvOpsData[0]['ops_str']);
        }
        if( $atvOpsData[1]['ops_str'] )
        {
            $atvOpsData_opsStr_1 = explode("#",$atvOpsData[1]['ops_str']);
        }
        //echo $atvOps->getLastSql();
        //echo dump($atvOpsData);

        //处理表头
        $header=array();
        $item['name']='主键';//0
        $item['type']='normal';
        array_push($header,$item);
        $item['name']='是否被选择';//1
        $item['type']='normal';
        array_push($header,$item);
        $item['name']='姓名';//2
        $item['type']='normal';
        array_push($header,$item);
        $item['name']='手机号';//3
        $item['type']='normal';
        array_push($header,$item);
        $item['name']='职业';//4
        $item['type']='normal';
        array_push($header,$item);
        $item['name']='毕业院校';//5
        $item['type']='normal';
        array_push($header,$item);
        $item['name']='学历';//6
        $item['type']='normal';
        array_push($header,$item);
        $item['name']=$atvOpsData[0]['question'];//第一个问题,7
        $item['type']='selectOne';
        $item['option'] = $atvOpsData_opsStr_0;//explode("#",$atvOpsData[0]['ops_str']);
        array_push($header,$item);
        $item['name']=$atvOpsData[1]['question'];//第二个问题,8
        $item['type']='selectOne';
        $item['option'] = $atvOpsData_opsStr_1;//explode("#",$atvOpsData[1]['ops_str']);
        array_push($header,$item);
        $item['name']='报名时间';//9
        $item['type']='normal';
        array_push($header,$item);
        //echo dump($header);


        //查询报名用户总个数，查询用户报名表
        $suUser = M('SignUpUser');
        $sqlCount = "SELECT count(*) as atvUserCount "
            ."FROM fj_atv_user_list a "
            ."left join fj_atv_user_regis_value b on a.id=b.user_regis_id and b.index=1 "
            ."left join fj_atv_user_regis_value c on a.id=c.user_regis_id and c.index=2 "
            ."where a.atv_id='$atvId' ";
        $sql = "SELECT a.id, a.if_pick_on, a.name, a.tel, b.value as answer0, c.value as answer1,a.sign_up_date "
            ."FROM fj_atv_user_list a "
            ."left join fj_atv_user_regis_value b on a.id=b.user_regis_id and b.index=1 "
            ."left join fj_atv_user_regis_value c on a.id=c.user_regis_id and c.index=2 "
            ."where a.atv_id='$atvId' ";
        if($selectMode=='selectedmode')
        {
            //设置选择模式，是否返回已勾选的用户
            $str_if_pick_on = "and a.if_pick_on=1 ";
            $sql = $sql.$str_if_pick_on;
            $sqlCount = $sqlCount.$str_if_pick_on;
        }
        if($filterCdn)//若问题的筛选条件不为空
        {
            $filterCdnJson = json_decode($filterCdn);

            $str_value = $filterCdnJson->value;
            $str_filter[7]="and b.value='$filterCdnJson->value' ";
            $str_filter[8]="and c.value='$filterCdnJson->value' ";

            $sql = $sql.$str_filter[$filterCdnJson->index];
            $sqlCount = $sqlCount.$str_filter[$filterCdnJson->index];
        }
        if($orderCdn)//若排序条件不为空
        {
            $str_order=array("a.id", "a.if_pick_on","a.name", "a.tel",
                "", "", "", "b.value", "c.value", "a.sign_up_date");
            $orderCdnJson = json_decode($orderCdn);
            $str_type = $orderCdnJson->type=="desc"?"desc":"asc";
            $str_field = $str_order[$orderCdnJson->index];
            if($str_field)
            {
                $str_orderSql = " order by $str_field $str_type ";
                $sql = $sql.$str_orderSql;
                $sqlCount = $sqlCount.$str_orderSql;
            }

        }
        if($pageNum>0)
        {
            $start = $pageSize*($pageNum-1);
            $sql = $sql."limit $start, $pageSize ";
        }
        $totalNum = $suUser->query($sqlCount);//查询总个数
        $totalNum = $totalNum[0]['atvUserCount'];
        //echo "totalNum: ".$totalNum;
        $userList = $suUser->query($sql);//查询报名用户
        //如果问题有选项，把查出的答案索引修改为实际答案
        if( count($atvOpsData_opsStr_0)!=0 && count($atvOpsData_opsStr_1)!=0 )
        {
            for($i=0;$i<count($userList);$i++)
            {
                $userList[$i]['answer0'] = $atvOpsData_opsStr_0[$userList[$i]['answer0']];
                $userList[$i]['answer1'] = $atvOpsData_opsStr_1[$userList[$i]['answer1']];
            }
        }

        //把userList转化成数组
        $userResultList = array();
        foreach ($userList as $userOne)
        {
            $userResultOne = array();
            array_push($userResultOne, $userOne['id'], $userOne['if_pick_on'], $userOne['name'],$userOne['tel'],
                '','','',$userOne['answer0'], $userOne['answer1'], $userOne['sign_up_date']);
            array_push($userResultList, $userResultOne);
        }

        //echo $suUser->getLastSql();
        //echo dump($userResultList);

        $userNum = count($userList);
        //echo $userNum;

        $dataResult = array();
        $dataResult['pageNum'] = $pageNum;
        $dataResult['pageSize'] = $pageSize;
        $dataResult['totalNum'] = $totalNum;
        $dataResult['header'] = $header;
        $dataResult['userNum'] = $userNum;
        $dataResult['userList'] = $userResultList;
        //echo dump($dataResult);

        $this->ajaxReturn(output_json_success($dataResult));
    }

    /**
     * 接口名称：m=Activity&a=modifySignUpUserSelect修改报名用户的选择状态
     * param :
     * atvId: //活动id
     * ids:[ 1,2,3,4,5,6,9] //活动名单表fj_atv_user_list的主键
     * selectMode: //选中--1，未选中--0
     */
    public function modifySignUpUserSelect($atvId,$ids,$selectMode)
    {
        $data_id_list = array();
        $data_id_json = json_decode($ids);
        $data_id_json_len = count($data_id_json);
        for ($i = 0; $i < $data_id_json_len; $i++)
        {
            array_push($data_id_list, $data_id_json[$i]);
        }

        $suUser = D('SignUpUser');
        $condition['id']  = array('in',$data_id_list);
        $condition['atv_id'] = $atvId;
        if($selectMode==1)
        {
            $data['if_pick_on']=1;
        }
        else if($selectMode==0)
        {
            $data['if_pick_on']=0;
        }
        else
        {
            //参数错误
            $this->ajaxReturn(output_json_fail('70010',''));
        }
        $suUser->where($condition)->save($data);
        //echo $suUser->getLastSql();

        $this->ajaxReturn(output_json_success(''));
    }

    /**
     * 接口名称：m=Activity&a=removeAtvUser移除报名用户（加一个enable的字段来判断是否可用，移除之后就修改这个字段为不可用）
     * param :
     * atvId: //活动id
     * ids:[ 1,2,3,4,5,6,9] //活动名单表fj_atv_user_list的主键
     */
    public function removeAtvUser($atvId,$ids)
    {
        $data_id_list = array();
        $data_id_json = json_decode($ids);
        $data_id_json_len = count($data_id_json);
        for ($i = 0; $i < $data_id_json_len; $i++)
        {
            array_push($data_id_list, $data_id_json[$i]);
        }

        $suUser = D('SignUpUser');
        $condition['id']  = array('in',$data_id_list);
        $condition['atv_id'] = $atvId;
        $data['enable']=0;
        $suUser->where($condition)->save($data);
        //echo $suUser->getLastSql();

        $this->ajaxReturn(output_json_success(''));
    }
}


?>